using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Grid;
using Sedna.Core.Controls.Support;
using Sedna.Core.Globalization;
using Iesi.Collections;
using ControlBuilder=Sedna.Core.Controls.Support.ControlBuilder;

namespace Sedna.Core.Controls.Template
{
    public class InvalidSighColumnTemplate : ItemColumnTemplate
    {
        public InvalidSighColumnTemplate(DataGrid dataGrid, 
                                       IDataGridColumnConfiguration columnConfiguration)
            : base(dataGrid, columnConfiguration)
		{
        }


        public override void InstantiateIn(Control container)
        {
            Image image = (Image) ControlBuilder.BuildControl(columnConfiguration.ImageConfiguration);

            image.EnableViewState = false;
            image.DataBinding += new EventHandler(BindImage);
            
            container.Controls.Add(image);
        }

        protected void BindImage(object sender, EventArgs e)
        {
            DataGridItem dataGridItem = (DataGridItem)((Control)sender).NamingContainer;
            Image image = (Image) sender;
            if (dataGrid.Controller.ErrorMessageKeyData.Contains(dataGridItem.DataItem))
            {
                IDictionary messageKeys = (IDictionary)dataGrid.Controller.ErrorMessageKeyData[dataGridItem.DataItem];
                
                if (messageKeys.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (DictionaryEntry msg in messageKeys)
                    {
                        if (msg.Value == null)
                            sb.Append(columnConfiguration.GetLocalizedPostfixLabel(msg.Key.ToString()));
                        else
                            sb.Append(
                                String.Format(columnConfiguration.GetLocalizedPostfixLabel(msg.Key.ToString()),
                                              (object[])msg.Value));
                        
                        sb.Append(Environment.NewLine);
                    }
                    if (sb.Length > 0)
                        sb.Length -= Environment.NewLine.Length;
                    
                    image.ToolTip = sb.ToString();
                    image.Visible = (columnConfiguration.IsVisibleForPrincipal() && columnConfiguration.ColumnControlEnabler.CheckIfVisible(columnConfiguration, dataGridItem.DataItem, ExecutionContext.Current));
                }
                else
                    image.Visible = false;
            }
            else
            {
                image.Visible = false;
            }
            image.Enabled = (columnConfiguration.IsEnabledForPrincipal() && columnConfiguration.ColumnControlEnabler.CheckIfEnabled(columnConfiguration, dataGridItem.DataItem, ExecutionContext.Current));
        }

    }
}
